import { getRange } from '@/ayaka'
import { vocabulary } from './vocabulary'

/**
 * 背单词
 */
export const creepReciteWord = (creeps: Creep[], p = 0.5) => {
    const rooms = new Set<Room>()
    // 先统计每个房间的爬
    creeps.forEach((creep) => {
        if (!creep.room.myCreeps) {
            creep.room.myCreeps = []
        }
        creep.room.myCreeps.push(creep)
        rooms.add(creep.room)
    })

    // 遍历每个房间
    for (const room of rooms) {
        if (Math.random() > p) break

        const roomCreeps = room.myCreeps || []
        const word = Math.floor(Math.random() * vocabulary.length)
        let index = 0
        const length = vocabulary[word].length
        for (const creep of roomCreeps) {
            creep.say(vocabulary[word][index++], true)
            if (index >= length) break
        }
    }
}

declare global {
    interface Room {
        myCreeps: Creep[]
    }
}
